﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    public class GameContentData : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private AnimationResourceData _selectedResource;
        public AnimationResourceData SelectedResource
        {
            get { return _selectedResource; }
            set { _selectedResource = value; OnPropertyChanged("SelectedResource"); }
        }
        public NotifiableCollection<AnimationResourceData> ResourceDisplayCollection { get { return ResourceItemDataDictionary.ValueCollection; } }
        private NotifiableDictionary<ResourceBase, AnimationResourceData> ResourceItemDataDictionary { get; set; }

        public GameContentData()
        {
            ResourceItemDataDictionary = new NotifiableDictionary<ResourceBase, AnimationResourceData>();
        }

        public void Load()
        {
            foreach (string resourceName in GameEngine.GetResourceItemList().Where(x => x != "obj").ToArray())
            {
                ResourceBase resourceItem = ResourceManager.Instance.LoadResource(resourceName);
                ResourceItemDataDictionary.Add(resourceItem, new AnimationResourceData(resourceItem.Name));
            }
        }

        public void SaveResourceItems()
        {
            foreach (AnimationResourceData animationResourceData in ResourceItemDataDictionary.ValueCollection)
                animationResourceData.Save();

            ContentBuilder contentBuilder = new ContentBuilder();
            contentBuilder.Save();
            contentBuilder.Clear();
        }

        public AnimationResourceData ObtainResourceData(ResourceBase resourceItem)
        {
            if (resourceItem == null)
                return null;

            if (!ResourceItemDataDictionary.ContainsKey(resourceItem))
            {
                AnimationResourceData resource = new AnimationResourceData(resourceItem.Name);
                ResourceItemDataDictionary.Add(resourceItem, resource);
                return resource;
            }
            else
            {
                return ResourceItemDataDictionary[resourceItem];
            }
        }

        public void RemoveResource(AnimationResourceData resourceItemData)
        {
            AnimationResource animationResource = ResourceManager.Instance[resourceItemData.ResourceName] as AnimationResource;
            ResourceItemDataDictionary.Remove(animationResource);
            ResourceManager.Instance.RemoveResource(resourceItemData.ResourceName);
            if (Directory.Exists(animationResource.ResourcePath))
                Directory.Delete(animationResource.ResourcePath, true);
            animationResource.Dispose();
        }

        public AnimationResourceData CreateNewResource()
        {
            ResourceBase resource = new AnimationResource(GetUniqueName());
            ResourceManager.Instance.AddResourceItem(resource);

            AnimationResourceData resourceItemData = new AnimationResourceData(resource.Name);
            ResourceItemDataDictionary.Add(resource, resourceItemData);

            return resourceItemData;
        }

        private string GetUniqueName()
        {
            int count = 0;
            string resourceName = "Resource" + count++;

            while (ResourceManager.Instance.Contains(resourceName))
                resourceName = "Resource" + count++;

            return resourceName;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
